$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'school/school/list',
        datatype: "json",
        // postData:{province:"其他"},
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '学校代码', name: 'code', index: 'code', width: 80},
            {label: '学校名称', name: 'name', index: 'name', width: 80},
            {label: '学校性质', name: 'nature', index: 'nature', width: 80},
            {label: '所在区域', name: 'area', index: 'area', width: 80},
            {label: '所在省份', name: 'province', index: 'province', width: 80},
            {label: '所在市', name: 'cities', index: 'province', width: 80},
            {label: '院校分类（普通,医学，艺术,军警体）', name: 'category', index: 'professionalCategory', width: 80},
            {label: '层次（1本科，2专科，3本专混合）', name: 'level', index: 'level', width: 80},
            {label: '包含专业形式（1普通，2校企合作，3中外合作, 4 3+2连读）', name: 'learntype', index: 'learnType', width: 80},
            {label: '推荐理由', name: 'recommendedreasons', index: 'recommendedReasons', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            region: "",
            province: "",
            city: "",
            nature: "",
            level: ""
        },
        showList: true,
        title: null,
        school: {},
        learntype: [],
        professionalcategory: [],
        areas: [
            {
                name: "华北",
                provinces: []
            },
            {
                name: "东北",
                provinces: []
            },
            {
                name: "华东",
                provinces: []
            },
            {
                name: "华中",
                provinces: []
            },
            {
                name: "西南",
                provinces: []
            },
            {
                name: "西北",
                provinces: []
            },
            {
                name:"华南",
                provinces:[]
            }


        ],
        chooseArea: [],
        chooseProvinces: [],
        provinces: [],
        cities: [],
        chooseCities: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.school = {
                nature: "公办",
                area: '',
                enrolmentmethod: "夏考",
                professionalcategory: "普通",
                level: "本科",
                learntype: "",
                province: "其他",


            };
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var learntype = ""
            var professionalcategory = ""
            var areaA = ""
            var areaA = ""
            var region = ""
            var province = ""
            var cities = ""
            for (var i = 0; i < vm.learntype.length; i++) {
                learntype = learntype + vm.learntype[i] + ","
            }
            for (var i = 0; i < vm.professionalcategory.length; i++) {
                professionalcategory = professionalcategory + vm.professionalcategory[i] + ","
            }
            for (var i = 0; i < vm.chooseArea.length; i++) {
                areaA = areaA + vm.chooseArea[i] + ","
                region = region + vm.chooseArea[i] + ","
            }
            for (var i = 0; i < vm.chooseProvinces.length; i++) {
                province = province + vm.chooseProvinces[i] + ","
            }
            for (var i = 0; i < vm.chooseCities.length; i++) {
                cities = cities + vm.chooseCities[i] + ","
            }
            vm.school.learntype = learntype
            vm.school.category = professionalcategory
            vm.school.area = areaA
            vm.school.region = region
            vm.school.province = province
            vm.school.cities = cities
            var url = vm.school.id == null ? "school/school/save" : "school/school/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.school),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                            vm.chooseArea=[]
                            vm.chooseProvinces=[]
                            vm.chooseCities=[]
                            vm.learntype=[]
                            vm.professionalcategory=[]
                            vm.areas[0].provinces=[]
                            vm.areas[1].provinces=[]
                            vm.areas[2].provinces=[]
                            vm.areas[3].provinces=[]
                            vm.areas[4].provinces=[]
                            vm.areas[5].provinces=[]
                            vm.areas[6].provinces=[]
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "school/school/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "school/school/info/" + id, function (r) {
                vm.school = r.school;
                vm.learntype = r.school.learntype.split(",");
                vm.professionalcategory = r.school.category.split(",");
                vm.area = r.school.area.split(",");

            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page,
                postData: {q: vm.q},
            }).trigger("reloadGrid");
        },
        setLastThreeYear: function () {
            var year = ["2018", "2017", "2016"]
            confirm('确定要设置近三年？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "school/school/setLastThreeYear",
                    contentType: "application/json",
                    data: JSON.stringify(year),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getProvinces(name, index) {
            pa = {
                'name': name
            }
            $.ajax({
                type: "POST",
                url: baseURL + "school/provinceAndCities/getProvinces",
                contentType: "application/json",
                data: JSON.stringify(pa),
                success: function (r) {
                    if (r.code === 0) {
                        vm.areas[index].provinces = r.data
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        getCities(name, index, i) {
            pa = {
                'name': name
            }
            $.ajax({
                type: "POST",
                url: baseURL + "school/provinceAndCities/getCities",
                contentType: "application/json",
                data: JSON.stringify(pa),
                success: function (r) {
                    if (r.code === 0) {
                        vm.areas[index].provinces[i].cities = r.data
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        ChangeRegion() {
            pa = {
                'name': vm.q.region
            }
            $.ajax({
                type: "POST",
                url: baseURL + "school/provinceAndCities/getProvinces",
                contentType: "application/json",
                data: JSON.stringify(pa),
                success: function (r) {
                    if (r.code === 0) {
                        vm.provinces = r.data
                        vm.reload()
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        ChangeProvince() {
            pa = {
                'name': vm.q.province
            }
            $.ajax({
                type: "POST",
                url: baseURL + "school/provinceAndCities/getCities",
                contentType: "application/json",
                data: JSON.stringify(pa),
                success: function (r) {
                    if (r.code === 0) {
                        vm.cities = r.data
                        vm.reload()
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
    }
});